'''Some concept of neighbourhood is useful to a distributed system. Whether 
it's for gossipping, or pinging nearby nodes to ensure they're down, or peer to
peer communication.

This module provides a place to store a persistent, generic neighbourhood. No 
bias towards application is made - this neighbourhood is probably just the 
entire node list. The only guarantees provided are that it gets updated once
in a while, and that any two neighbourhoods with the same time stamp will be
identical in content and ordering even if they're on two different nodes.
'''
import cPickle
import conf

CACHE_FILE=conf.CACHE_FILE # persistent storage
stamp=0  #4-byte unique timestamp
nodes=[] #string list of host names, might be empty

def set(timestamp, neighbours):
    '''Sets a new neighbourhood and time stamp.'''
    stamp,nodes = timestamp,neighbours
    print 'NEIGHBOUR FILE',CACHE_FILE
    cPickle.dump((stamp,nodes), open(CACHE_FILE, 'wb'))

try:
    stamp,nodes = cPickle.load(open(CACHE_FILE, 'rb'))
except:
    # file probably doesn't exist - that's absolutely fine
    pass
